package wavedigest;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wavedigest.data.EmailRegistration;
import wavedigest.data.PMF;
import wavedigest.data.RegisteredWave;

import com.google.appengine.repackaged.com.google.common.util.Base64;
import com.google.appengine.repackaged.com.google.common.util.Base64DecoderException;

@SuppressWarnings("serial")
public class GetRegistrationsServlet extends HttpServlet
{
	private static final Logger log = Logger.getLogger(GetRegistrationsServlet.class.getName());
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		String wid = null;
		try
		{
			wid = new String(Base64.decode(req.getParameter("wid").getBytes()));
		}
		catch (Base64DecoderException e)
		{
			e.printStackTrace();
			wid = null;
		}
		
		if (null == wid)
			throw new ServletException("expected a wave id");
		
		log.info("wid = " + wid);

		getRegistrationsHTML(wid, resp);
	}
	
	@SuppressWarnings("unchecked")
	private void getRegistrationsHTML(String wid, HttpServletResponse resp) throws IOException
	{
		log.info("getting registrations for wave id:" + wid);

		StringBuilder html = new StringBuilder();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			String query = "select from " + RegisteredWave.class.getName() + " where waveID == '" + wid + "'";
			log.info("query=" + query);
			List<RegisteredWave> waveList = (List<RegisteredWave>) pm.newQuery(query).execute();
			log.info("results=" + waveList.size());

			if (waveList.isEmpty())
			{
				log.severe("Wave was not registered: " + wid);
				return;
			}
			
			RegisteredWave regWave = waveList.get(0);
			log.info("num registrations=" + regWave.getRegistrations().size());
			if (regWave.getRegistrations().size() > 0)
			{
				html.append("<ul>");
				for (EmailRegistration registration : regWave.getRegistrations())
				{
					html.append("<li>" + registration.getEmailAddress() + " - " + getScheduleTypeName(registration.getScheduleType()) + "</li>");
				}
				html.append("</ul>");
			}
		}
		finally
		{
			pm.close();
		}
		
		resp.getOutputStream().write(html.toString().getBytes());
	}

	private String getScheduleTypeName(int scheduleType)
	{
		switch (scheduleType)
		{
			case EmailRegistration.SCHEDULE_TYPE_DAILY:
				return "daily";
			case EmailRegistration.SCHEDULE_TYPE_EVERY_CHANGE:
				return "every change";
			case EmailRegistration.SCHEDULE_TYPE_HOURLY:
				return "hourly";
			case EmailRegistration.SCHEDULE_TYPE_WEEKLY:
				return "weekly";
		}
		return "unknown";
	}
}
